package com.pocketsweet.ui.uilib;

import com.pocketsweet.model.MLUser;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MixListView extends AutoListView {
	private SlideView mFocusedItemView;

	public MixListView(Context context) {
		super(context);
	}

	public MixListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MixListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public void shrinkListItem(int position) {
		View item = getChildAt(position);

		if (item != null) {
			try {
				((SlideView) item).shrink();
			} catch (ClassCastException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int x = (int) event.getX();
		int y = (int) event.getY();
		int position = pointToPosition(x, y);
//		int position = pointToPosition(x, y) - 1;  //恋爱呼叫
		if (getItemAtPosition(position) == null) {
			return super.onTouchEvent(event);
		}
		if (position != INVALID_POSITION
				&& ((MLUser) getItemAtPosition(position)).slide != null
				&& !((MLUser) getItemAtPosition(position)).slide
						.equals(mFocusedItemView)) {
			if (mFocusedItemView != null) {
				mFocusedItemView.shrink();
			}
		}
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			if (position != INVALID_POSITION) {
				MLUser user = (MLUser) getItemAtPosition(position);
				mFocusedItemView = user.slide;
			}

		}
			break;
		case MotionEvent.ACTION_MOVE: {
			// super.whenMove(event);
			if (position != INVALID_POSITION) {
				MLUser user = (MLUser) getItemAtPosition(position);
				mFocusedItemView = user.slide;

				// mFocusedItemView.requestDisallowInterceptTouchEvent(false);
			}

		}
			break;

		default:
			break;
		}
		// 向当前点击的view发送滑动事件请求，其实就是向SlideView发请求
		if (mFocusedItemView != null) {
			mFocusedItemView.onRequireTouchEvent(event);
		}

		return super.onTouchEvent(event);
	}

}
